package com.th.supcom.pds.recipefee.client.page;

import net.carefx.fc.component.editor.client.EMREditorPanel;
import net.carefx.fc.component.editor.client.command.Command.CommandBuilder;
import net.carefx.fc.component.editor.client.data.InitListener;
import net.carefx.fc.component.editor.client.utils.ActivexUtils;
import net.carefx.framework.container.client.context.AppContext;
import net.carefx.framework.container.client.context.ContextValueCannotChangedException;
import net.carefx.framework.container.client.page.IPageEntryPoint;
import net.carefx.framework.container.client.page.PageContext;
import net.carefx.framework.reflection.shared.annotations.Reflectable;

import com.extjs.gxt.ui.client.Style.HideMode;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.th.supcom.pds.common.enums.ComponentType;
import com.th.supcom.pds.common.enums.TJToolCommand;

/**
 * 欢迎页面
 */

@Reflectable
public class EntryPoint implements IPageEntryPoint
{
    private Button btn10;
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    private EMREditorPanel m_emrEditorPanel;
    private Label lable;

    @Override
    public void start (final PageContext context)
    {
        context.getWorkBench ().getBodyView ().getWorkspace ().getContextView ().asLayoutContainer ().hide ();
        Button btn1 = createButton (null, "门诊西药房", "0106190510");
        Button btn2 = createButton (null, "门诊五楼西药房", "0106190511");
        Button btn3 = createButton (null, "中药房", "0106190502");
        Button btn4 = createButton (null, "第二门诊西药房", "0106190508");
        Button btn5 = createButton (null, "门诊四楼中药房", "0106190507");
        Button btn6 = createButton (null, "门诊三楼西药房", "0106190509");
        Button btn7 = createButton (null, "住院部夜班药房", "0106190521");
        Button btn8 = createButton (null, "消化内科护理单元", "0102270501");
        Button btn9 = createButton (null, "呼吸内科护理单元", "0102270504");
        btn10 = new Button ("操作员Id");
        btn10.setId ("000666");
        btn10.setTitle ("000666");
        LayoutContainer mainLayoutContainer;
        mainLayoutContainer = context.getContainer ();
        mainLayoutContainer.setLayout (new FitLayout ());
        mainLayoutContainer.setHideMode (HideMode.OFFSETS);

        ContentPanel contentPanel = new ContentPanel ();
        contentPanel.setHeaderVisible (false);
        contentPanel.setButtonAlign (HorizontalAlignment.CENTER);
        contentPanel.add (btn1);
        contentPanel.add (btn2);
        contentPanel.add (btn3);
        contentPanel.add (btn4);
        contentPanel.add (btn5);
        contentPanel.add (btn6);
        contentPanel.add (btn7);
        contentPanel.add (btn8);
        contentPanel.add (btn9);
        contentPanel.add (btn10);
        lable = new Label ();
        lable.setSize (60, 30);
        contentPanel.add (lable);

        m_emrEditorPanel = new EMREditorPanel ("pds_nopaid_ip" + (ActivexUtils.getEMREditorIndex ()), ComponentType.TJTool);
        m_emrEditorPanel.setVisible (false);
        addListeners ();

        mainLayoutContainer.add (contentPanel);
        mainLayoutContainer.add (m_emrEditorPanel);
    }

    @Override
    public void stop (final PageContext context)
    {
        return;
    }

    private Button createButton (Button btn, final String title, String deptCode)
    {
        if (btn == null)
        {
            btn = new Button ();
        }
        btn.setText (title);
        btn.setStyleAttribute ("float", "left");
        btn.setStyleAttribute ("margin-left", "20px");
        btn.setStyleAttribute ("margin-top", "20px");
        btn.setItemId (deptCode);
        btn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                lable.setText ("当前登陆科室为:" + title);
                String btnId = ce.getButton ().getItemId ();
                try
                {
                    AppContext.get ().register ("deptCode", btnId);
                    AppContext.get ().register ("deptName", title);

                }
                catch (ContextValueCannotChangedException e)
                {
                    e.printStackTrace ();
                }
            }
        });
        return btn;

    }

    private void addListeners ()
    {
        btn10.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                String btnId = ce.getButton ().getItemId ();
                try
                {
                    AppContext.get ().register ("operator", btnId);

                }
                catch (ContextValueCannotChangedException e)
                {
                    e.printStackTrace ();
                }
            }
        });
        m_emrEditorPanel.setHideMode (HideMode.OFFSETS);
        m_emrEditorPanel.setListener (new InitListener ()
        {
            @Override
            public void afterInit ()
            {
                final String arg = m_emrEditorPanel.syncExecute (new CommandBuilder (TJToolCommand.TOOL_GETSYSINFO).create ());
                JSONValue jSONValue = JSONParser.parseLenient (arg);
                JSONObject object = jSONValue.isObject ();
                final String args = object.get ("args").isString ().stringValue ();
                jSONValue = JSONParser.parseLenient (args);
                object = jSONValue.isObject ();
                String IP = object.get ("ip[0]").isString ().stringValue ();
                try
                {
                    AppContext.get ().register ("IP", IP);
                }
                catch (ContextValueCannotChangedException e)
                {
                    e.printStackTrace ();
                }
            }
        });
    }
}
